<template>
  <div class="login">
    <div class="loall">
      <div class="yjlogo">
        <img src="../assets/yjlogo.png" alt="" />
        <p>粤建职校在线练习</p>
      </div>
      <div class="info">
        <div class="info-main">
          <div class="same">
            <input
              type="text"
              placeholder="请输入您的手机号码"
              ref="inputtell"
              @blur="backbottom"
            />
          </div>
          <div class="same">
            <input
              type="text"
              placeholder="请输入验证码"
              ref="inputverify"
              @blur="backbottom"
            />
            <div class="verify" v-show="show" @click="countdown">
              获取验证码
            </div>
            <div class="verify" v-show="!show">{{ count }}S重新获取</div>
          </div>
          <div class="same">
            <div class="tologin" @click="login">绑定手机号码</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      show: true,
      count: "",
      tival: null,
      resu: null,
      resu1: null,
      apiUrl: "/api/mzcy/login/sendSms?phone=",
      apiUrl1: "/api/mzcy/login/login",
    };
  },
  methods: {
    countdown: function () {
      this.resu = this.$refs.inputtell.value;
      if (this.resu == "") {
        // alert('手机号不能为空')
        Toast("请先输入手机号");
        return false;
      } else if (
        !/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/.test(
          this.resu
        )
      ) {
        Toast("请输入正确的手机号");
        return false;
      } else {
        this.$http.get(this.apiUrl + this.resu).then(
          function (res) {
            // console.log(res.data.code);
            res.data.code != 1 ? Toast("发送失败") : Toast("发送成功");
            const countDownNum = 60;
            this.count = countDownNum;
            this.show = false;
            this.tival = setInterval(() => {
              this.count--;
              if (this.count <= 0) {
                clearInterval(this.tival);
                this.show = true;
              }
            }, 1000);
            // 响应成功回调
          },
          function (res) {
            // 响应错误回调
          }
        );
      }
    },
    login: function () {
      this.resu = this.$refs.inputtell.value;
      this.resu1 = this.$refs.inputverify.value;
      if (this.resu == "") {
        // alert('手机号不能为空')
        Toast("手机号不能为空");
        return false;
      } else if (
        !/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/.test(
          this.resu
        )
      ) {
        Toast("请输入正确的手机号");
        return false;
      } else if (this.resu1 == "") {
        Toast("验证码不能为空");
        return false;
      } else if (this.resu1.length != 6) {
        Toast("请输入正确的验证码");
        return false;
      } else {
        this.$http
          .get(this.apiUrl1 + "?phone=" + this.resu + "&code=" + this.resu1)
          .then(
            function (res) {
              // console.log(res.data.code);
              res.data.code != 1
                ? Toast("验证码错误")
                : this.$router.push({ path: "home" });
              // 响应成功回调
            },
            function (res) {
              // 响应错误回调
            }
          );
      }
    },
    backbottom: function () {
      setTimeout(() => {
        window.scroll(0, 0);
      }, 200);
    },
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  /* height: 100%; */
  /* min-height: 100vh; */
  /* position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0; */
  background: #ffffff;
}
.loall {
  width: 100%;
  min-height: 100vh;
  background: url(../assets/u8.jpg);
  background-repeat: no-repeat;
  background-size: contain;
  background-position-y: bottom;
  overflow: hidden;
}
.yjlogo {
  width: 90%;
  margin: 0 auto;
  padding-top: 0.8rem;
  padding-bottom: 1rem;
  border-bottom: 3px solid #1c5ac8;
}
.yjlogo p {
  font-size: 1.8rem;
  font-weight: bold;
  color: #424d61;
}
.info {
  width: 100%;
}
.info-main {
  width: 80%;
  margin: 0 auto;
}
.same {
  width: 100%;
  margin: 2rem 0;
  overflow: hidden;
}
.same input {
  width: 100%;
  height: 2.7rem;
  border: none;
  outline: none;
  background: #f2f2f2;
  border-radius: 8px;
  font-size: 1rem;
  text-indent: 0.6rem;
}
.same:nth-child(2) input {
  width: 60%;
  float: left;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.verify {
  width: 40%;
  height: 2.7rem;
  line-height: 2.7rem;
  float: left;
  border-top-right-radius: 8px;
  border-bottom-right-radius: 8px;
  background: #1c5ac8;
  color: #ffffff;
}
.tologin {
  width: 100%;
  height: 2.7rem;
  color: #ffffff;
  background: #1c5ac8;
  border-radius: 8px;
  line-height: 2.7rem;
  font-size: 1rem;
}
</style>
